package club.gskj.gulimall.coupon.service.impl;

import club.gskj.gulimall.coupon.entity.SeckillSkuRelationEntity;
import club.gskj.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import club.gskj.common.utils.PageUtils;
import club.gskj.common.utils.Query;

import club.gskj.gulimall.coupon.dao.SeckillSessionDao;
import club.gskj.gulimall.coupon.entity.SeckillSessionEntity;
import club.gskj.gulimall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLates3DaysSession() {

        // 这里需要计算时间 最近三天的时间

        // 格式化日期时间
        String startTimeStr = startTime();
        String endTimeStr = endTime();

        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTimeStr, endTimeStr));

        if (null != list && list.size() > 0) {
            // 重新设置当前秒杀对象所关联的 sku信息
            List<SeckillSessionEntity> collect = list.stream().map(item -> {
                // 当前活动id
                Long id = item.getId();
                // 查询当前秒杀活动关联的商品信息
                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                item.setRelationEntities(relationEntities);
                return item;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    private String startTime() {
        // 当天
        LocalDate now = LocalDate.now();
        // 获取最大时间、最小时间
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now, min);
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private String endTime() {
        // 当天+2天。第三天
        LocalDate now = LocalDate.now().plusDays(2);

        // 获取最大时间、最小时间
        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(now, max);
        return end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}
